import { createRouter, createWebHashHistory, createWebHistory } from "vue-router"
import store from "../store/index"
const routes = [
  // 登录界面
  {
    path: "/login",
    name: "login",
    component: () => import("../views/pages/login.vue"),
  },

  // 功能路由
  {
    path: "/",
    name: "layout",
    component: () => import("../views/layout/LayOut.vue"),
    redirect: "/index",
    children: [
      {
        path: 'index',
        name: "index",
        component: import("../views/pages/index.vue")
      },
      {
        path: 'roules',
        name: "roules",
        component: import("../views/pages/rolesList.vue")
      },
      {
        path: 'user',
        name: "user",
        component: import("../views/pages/userlist.vue")
      },
      {
        path: 'goods',
        name: "goods",
        component: import("../views/pages/goodlist.vue")
      },
    ]
  },

]

const router = createRouter({
  //使用url的#符号之后的部分模拟url路径的变化,因为不会触发页面刷新,所以不需要服务端支持
  //history: createWebHashHistory(), 
  history: createWebHistory(),
  routes
})
router.beforeEach((to, from, next) => {
  // to从那个页面 from 到那个页面 next 只用执行next（）页面才会进行跳转
  // 判断用户是否登录
  console.log("staore", store.state.uInfo);
  const uInfo = store.state.uInfo.userInfo

  if (!uInfo.username) {
    if (to.path === "/login") {
      next()
      return
    }
    // 未登录进行跳转
    next('/login')
  } else {
    next()
  }

})
export default router